﻿using BaseUI;
using BLL;
using Model;
using System;
using System.Data;
using System.Web.UI.WebControls;

namespace Thksoft.UserCotrols
{
    public partial class CtrlMonthFundMX : BaseControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string busId = Request.QueryString["N_business_id"].ToString();

                hidId.Value = busId;
                ViewState["busId"] = busId.ToString();
                BindDt(busId);
            }
        }

        /// <summary>
        /// 绑定数据
        /// </summary>
        public void BindDt(string busId)
        {
            QS_Business bModel = new Qs_BusinessBLL().GetBusinessById(Decimal.Parse(busId));

            lblTime.Text = bModel.Business_operator_date.ToShortDateString();

            lblUser.Text = new Sys_UserBLL().GetInfoById(bModel.Business_operator_Id.ToString()).USER_NAME;

            string sql = string.Format(@" select * from QS_PLANMONTHAPPLY t where t.n_business_id=" + busId);

            DataTable dtYear = new BaseBLL().Query(sql);

            if (dtYear == null || dtYear.Rows.Count < 1)
            {
                Response.Write("<script>alert('【系统提示】该条数据有误，请与管理员联系');location.href='../Right.aspx';</script>");
                return;
            }

            lblyear.Text = dtYear.Rows[0]["C_USEMONTH"].ToString();

            lblBzyj.Text = dtYear.Rows[0]["C_REAMRK"].ToString();

            lbldw.Text = new Sys_CompanyBLL().GetInfoById(dtYear.Rows[0]["N_COMPANY_ID"].ToString()).CMY_NAME;

            this.hidId.Value = dtYear.Rows[0]["N_MONTHAPP_ID"].ToString() + "@" + busId;

            DataTable dt = new QS_PLANMONTHAPPLYBLL().GetDataBind(busId, lognUser.USER_NAME);

            HiddenField3.Value = dt.Rows[0]["n_monthapp_id"].ToString();

            gvMonthFunds.DataSource = dt;

            gvMonthFunds.DataBind();
        }

        protected void gvMonthFunds_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string type = ((Label)e.Row.FindControl("lbltype")).Text;
                var ifdetails = ((HiddenField)e.Row.FindControl("HiddenField1")).Value == "1" ? true : false;
                string sss = ((Label)e.Row.FindControl("lblProID")).Text;
                if (type == "0")
                {
                    e.Row.Attributes["style"] = "background-color:#B2D8FF;font-size: 14px";

                    // e.Row.Cells[2].Attributes["style"] = "font-size: 14px";
                }
                else if (type == "1")
                {
                    e.Row.Attributes["style"] = "background-color:#CCCCCC;font-size: 13px;";

                    //  e.Row.Cells[2].Attributes["style"] = "font-size: 13px";
                }
                else if (type == "2" && ifdetails)
                {
                    //查询详情表的值
                    //var applyId = Convert.ToInt32(HiddenField3.Value);
                    //var sql = "SELECT T1.N_AMOUNT FROM QS_PLANMONTH_DETAIL T1 WHERE T1.N_MONTHAPP_ID={0} AND T1.N_PROID={1}";
                    //sql = string.Format(sql, applyId, sss);
                    //var dt = new BaseBLL().Query(sql);
                    //if (dt.Rows.Count == 0)
                    //{
                    //    e.Row.Cells[4].Text = "0";
                    //    //e.Row.Cells[""]
                    //}
                    //else
                    //{
                    //    e.Row.Cells[4].Text = dt.Rows[0][0].ToString();
                    //}
                    var code = ((HiddenField)e.Row.FindControl("HiddenField2")).Value;

                    e.Row.Cells[4].Attributes.Add("onclick", "FORM_Details(" + HiddenField3.Value + ",2," + sss + ",'" + code + "')");
                    //((TextBox)e.Row.FindControl("txtNowMonth")).ReadOnly = true;
                }
            }
        }

        /// <summary>
        /// 重新加载数据(子表信息)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void ReLoadClick(object sender, EventArgs e)
        {
            foreach (GridViewRow row in gvMonthFunds.Rows)
            {
                string proId = ((Label)row.FindControl("lblProID")).Text;
                string type = ((Label)row.FindControl("lbltype")).Text;
                var ifdetails = ((HiddenField)row.FindControl("HiddenField1")).Value == "1" ? true : false;
                if (type == "2" && ifdetails)
                {
                    //查询详情表的值
                    //var applyId = Convert.ToInt32(HiddenField3.Value);
                    //var sql = "SELECT T1.N_AMOUNT FROM QS_PLANMONTH_DETAIL T1 WHERE T1.N_MONTHAPP_ID={0} AND T1.N_PROID={1}";
                    //sql = string.Format(sql, applyId, proId);
                    //var dt = new BaseBLL().Query(sql);
                    //if (dt.Rows.Count == 0)
                    //{
                    //    //((TextBox)row.FindControl("txtNowMonth")).Text = "0";
                    //    row.Cells[4].Text = "0";
                    //}
                    //else
                    //{
                    //    row.Cells[4].Text = dt.Rows[0][0].ToString();
                    //}
                    var code = ((HiddenField)row.FindControl("HiddenField2")).Value;
                    row.Cells[4].Attributes.Add("onclick", "FORM_Details(" + HiddenField3.Value + ",2," + proId + ",'" + code + "')");
                    //((TextBox)row.FindControl("txtNowMonth")).ReadOnly = true;
                }
            }
        }
    }
}